package com.ruoyi.eamp.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampCarLogs;
import com.ruoyi.eamp.domain.vo.EampCarListVO;
import com.ruoyi.eamp.domain.vo.EampListAndEampUse;
import com.ruoyi.eamp.mapper.EampCarLogsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eamp.mapper.EampCarListMapper;
import com.ruoyi.eamp.domain.EampCarList;
import com.ruoyi.eamp.service.IEampCarListService;

/**
 * 列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class EampCarListServiceImpl implements IEampCarListService
{
    @Autowired
    private EampCarListMapper eampCarListMapper;

    @Autowired
    private EampCarLogsMapper eampCarLogsMapper;


    /**
     * 查询列表
     *
     * @param carId 列表主键
     * @return 列表
     */
    @Override
    public EampCarList selectEampCarListByCarId(Long carId)
    {
        return eampCarListMapper.selectEampCarListByCarId(carId);
    }

    /**
     * 查询列表列表
     *
     * @param eampCarList 列表
     * @return 列表
     */
    @Override
    public List<EampCarListVO> selectEampCarListList(EampCarList eampCarList)
    {
        return eampCarListMapper.selectEampCarListList(eampCarList);
    }

    /**
     * 新增列表
     *
     * @param eampCarList 列表
     * @return 结果
     */
    @Override
    public int insertEampCarList(EampCarList eampCarList)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        eampCarList.setCarCreatePeople(user.getUserId());
        return eampCarListMapper.insertEampCarList(eampCarList);
    }

    /**
     * 修改列表
     *
     * @param eampCarList 列表
     * @return 结果
     */
    @Override
    public int updateEampCarList(EampCarList eampCarList)
    {
        Long carOwners = eampCarList.getCarOwners();
        Long carId = eampCarList.getCarId();
        Date date = new Date();
        EampCarLogs eampCarLogs = new EampCarLogs(null,1L,carOwners,carId,null,date,null,null,4L);
        eampCarLogsMapper.insertEampCarLogs(eampCarLogs);
        return eampCarListMapper.updateEampCarList(eampCarList);
    }

    /**
     * 批量删除列表
     *
     * @param carIds 需要删除的列表主键
     * @return 结果
     */
    @Override
    public int deleteEampCarListByCarIds(Long[] carIds)
    {

        //todo 使用记录生成:删除车辆的记录
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        for (Long carId : carIds) {
            EampCarLogs eampCarLogs = new EampCarLogs(null,1L,userId,carId,null,date,null,null,3L);
            eampCarLogsMapper.insertEampCarLogs(eampCarLogs);
        }
        int i = eampCarListMapper.updateEampCarListByCarIds(carIds);

        return i;
    }

    /**
     * 删除列表信息
     *
     * @param carId 列表主键
     * @return 结果
     */
    @Override
    public int deleteEampCarListByCarId(Long carId)
    {
        Date date = new Date();
        Long userId = SecurityUtils.getUserId();
        EampCarLogs eampCarLogs = new EampCarLogs(null,1L,userId,carId,null,date,null,null,3L);
        eampCarLogsMapper.insertEampCarLogs(eampCarLogs);
        int i = eampCarListMapper.updateEampCarListByCarId(carId);
        return i;
    }

    @Override
    public List<SysUser> getCreateById() {
        return eampCarListMapper.getCreateById();
    }

    @Override
    public List<EampListAndEampUse> getUseAndList() {
        return eampCarListMapper.getUseAndList();
    }


}
